home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / snippet.exe / UUENCODE.C < prev    next >
C/C++ Source or Header  |  1992-07-03  |  3KB  |  136 lines

  1. /* uuencode.c */
  2.  
  3. /*
  4. uudecode and uuencode are easily implemented under MSDOS as well.  Here
  5. are the sources for Microsoft C v3.0, but if you have another kind of C
  6. compiler, there should be perhaps only 1 change -- the output file of
  7. uudecode and the input file of uuencode must be in binary format.
  8. (ie.  binary files, like .EXE files may have byte patterns that are the
  9. same as ^Z, which signals end-of-file in non-binary (text) mode).
  10.  
  11.     Don Kneller
  12. UUCP:   ...ucbvax!ucsfcgl!kneller
  13. ARPA:   kneller@ucsf-cgl.ARPA
  14. BITNET: kneller@ucsfcgl.BITNET
  15. */
  16.  
  17. #ifndef lint
  18. static char sccsid[] = "@(#)uuencode.c  5.1 (Berkeley) 7/2/83";
  19. #endif
  20.  
  21. /*
  22.  * uuencode [input] output
  23.  *
  24.  * Encode a file so it can be mailed to a remote system.
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30.  
  31. /* ENC is the basic 1 character encoding function to make a char printing */
  32.  
  33. #define ENC(c) (((c) & 077) + ' ')
  34.  
  35. main(int argc, char *argv[])
  36. {
  37.       FILE *in;
  38.       struct stat sbuf;
  39.       int mode;
  40.  
  41.       /* optional 1st argument */
  42.  
  43.       if (argc > 2)
  44.       {
  45. #ifdef MSDOS
  46.             /* Use binary mode */
  47.             if ((in = fopen(argv[1], "rb")) == NULL)
  48.             {
  49. #else
  50.             if ((in = fopen(argv[1], "r")) == NULL)
  51.             {
  52. #endif
  53.                   perror(argv[1]);
  54.                   exit(1);
  55.             }
  56.             argv++; argc--;
  57.       }
  58.       else  in = stdin;
  59.  
  60.       if (argc != 2)
  61.       {
  62.             printf("Usage: uuencode [infile] remotefile\n");
  63.             exit(2);
  64.       }
  65.  
  66.       /* figure out the input file mode */
  67.  
  68.       fstat(fileno(in), &sbuf);
  69.       mode = sbuf.st_mode & 0777;
  70.       printf("begin %o %s\n", mode, argv[1]);
  71.  
  72.       encode(in, stdout);
  73.  
  74.       printf("end\n");
  75.       exit(0);
  76. }
  77.  
  78. /*
  79.  * copy from in to out, encoding as you go along.
  80.  */
  81.  
  82. encode(FILE *in, FILE *out)
  83. {
  84.       char buf[80];
  85.       int i, n;
  86.  
  87.       for (;;)
  88.       {
  89.             /* 1 (up to) 45 character line */
  90.  
  91.             n = fr(in, buf, 45);
  92.             putc(ENC(n), out);
  93.  
  94.             for (i = 0; i < n; i += 3)
  95.                   outdec(&buf[i], out);
  96.  
  97.             putc('\n', out);
  98.             if (n <= 0)
  99.                   break;
  100.       }
  101. }
  102.  
  103. /*
  104.  * output one group of 3 bytes, pointed at by p, on file f.
  105.  */
  106.  
  107. outdec(char *p, FILE *f)
  108. {
  109.       int c1, c2, c3, c4;
  110.  
  111.       c1 = *p >> 2;
  112.       c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  113.       c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  114.       c4 = p[2] & 077;
  115.       putc(ENC(c1), f);
  116.       putc(ENC(c2), f);
  117.       putc(ENC(c3), f);
  118.       putc(ENC(c4), f);
  119. }
  120.  
  121. /* fr: like read but stdio */
  122.  
  123. int fr(FILE *fd, char *buf, int cnt)
  124. {
  125.       int c, i;
  126.  
  127.       for (i = 0; i < cnt; i++)
  128.       {
  129.             c = getc(fd);
  130.             if (c == EOF)
  131.                   return(i);
  132.             buf[i] = c;
  133.       }
  134.       return (cnt);
  135. }
  136.